package TCPTest02;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    /**
     * 接收客户端的消息并发送一条消息到客户端
     * @param args
     */
    public static void main(String[] args) throws IOException {

        //1.创建对象ServerSocket
        ServerSocket ss = new ServerSocket(10000);


        //2、监听客户端的连接
        Socket socket = ss.accept();
        System.out.println(socket.getReceiveBufferSize());
        //3、从连接通道中获取输入流读取数据
        InputStream inputStream = socket.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(reader);
        int b;
        //read 方法会从连接通道读取数据
        //但是，需要一个结束标记，此处的循环才会停止
        while ((b=br.read())!= -1){
            System.out.print((char) b);
        }
        //4、写回数据
        String str = "到底有多开心";
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(str.getBytes());
        //5、 释放资源
        socket.close();
        inputStream.close();
    }
}
